package com.iwinding.ladygg.utils;

import android.app.Activity;
import android.content.Intent;
import android.text.TextUtils;
import android.widget.Toast;

import com.hyphenate.EMCallBack;
import com.hyphenate.chat.EMClient;
import com.iwinding.ladygg.ICaneApplication;
import com.iwinding.ladygg.constant.SPConstant;
import com.iwinding.ladygg.net.RequestManager;
import com.iwinding.ladygg.net.callback.LoginCallback;
import com.iwinding.ladygg.net.request.LoginReq;
import com.iwinding.ladygg.net.response.LoginRp;
import com.iwinding.ladygg.ui.act.BindPhoneActivity;
import com.iwinding.ladygg.ui.act.MainActivity;
import com.iwinding.ladygg.ui.view.ProDialogCreator;

/**
 * Created by 82218 on 2017/8/31.
 */

public class LoginUtil {

    public static void login(final Activity mContext, final LoginReq loginReq) {
        ProDialogCreator.getInstance().showDialog("Loading...");
        RequestManager.postLogin(mContext, loginReq, new LoginCallback(mContext) {
            @Override
            public void onResult(final LoginRp loginRp) {
                saveLoginInfo(mContext, loginRp, loginReq.password);
            }

            @Override
            public void onFail(String errorMsg) {
                ProDialogCreator.getInstance().dismissDialog();
                ToastUtil.openBaseToast(mContext, errorMsg, Toast.LENGTH_SHORT);
            }
        });
    }

    private static void saveLoginInfo(Activity mContext, LoginRp loginRp, String password) {
        loginRp.passwrod = password;
        ICaneApplication mOAAplication = (ICaneApplication) mContext.getApplicationContext();
        mOAAplication.setmLoginRp(loginRp);
        SPUtils.putSpProperty(mContext, SPConstant.IS_EXIT, false);
        SPUtils.putSpProperty(mContext, SPConstant.LOGIN_USER, GsonUtil.getInstance().toJson(loginRp));
        loginEM(mContext, loginRp);
    }

    private static void loginEM(final Activity mContext, final LoginRp loginRp) {
        if (!TextUtils.isEmpty(loginRp.userId)) {
            EMClient.getInstance().login(loginRp.userId, loginRp.passwrod, new EMCallBack() {//回调
                @Override
                public void onSuccess() {
                    ProDialogCreator.getInstance().dismissDialog();
                    new Thread(new Runnable() {
                        @Override
                        public void run() {
                            EMClient.getInstance().groupManager().loadAllGroups();
                            EMClient.getInstance().chatManager().loadAllConversations();
                        }
                    }).start();
                    if (!TextUtils.isEmpty(loginRp.isBindingPhone) && TextUtils.equals(loginRp.isBindingPhone, "0")) {
                        mContext.startActivity(new Intent(mContext, BindPhoneActivity.class));
                        return;
                    }
                    mContext.startActivity(new Intent(mContext, MainActivity.class));
                    mContext.finish();
                }

                @Override
                public void onProgress(int progress, String status) {
                }

                @Override
                public void onError(int code, String message) {
                    ProDialogCreator.getInstance().dismissDialog();
                    if (code == 200) {
                        mContext.startActivity(new Intent(mContext, MainActivity.class));
                        mContext.finish();
                    }
                }
            });
            ToastUtil.openLoginSucess(mContext);
        } else {
            ProDialogCreator.getInstance().dismissDialog();
        }
    }


}
